package com.sso.realm;


import com.common.entity.Client;

import com.sso.openFeign.BusinessOpenFeign;
import com.sso.token.HomeToken;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;


/**
 * @create 2022-08-04
 */
public class HomeMyRealm extends AuthorizingRealm {
    @Autowired
    private BusinessOpenFeign businessOpenFeign;


    @Override
    //授权功能
    //当你进行权限校验就会走该方法
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }

    @Override
    public boolean supports(AuthenticationToken token) {

        System.out.println(token instanceof HomeToken);
        System.out.println("走这个了");

        return token instanceof HomeToken;
    }

    @Override
    //认证功能
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        //1.获取登陆账号
        String mobile = authenticationToken.getPrincipal().toString();
        //2.根据账号查询用户信息
        Client client = businessOpenFeign.findClientByUsername(mobile);
        //3.判断用户是否存在
        if (client!=null){
            System.out.println(client);
            //4.判断密码是否错误
            ByteSource salt= ByteSource.Util.bytes(client.getSalt());
            SimpleAuthenticationInfo info=new SimpleAuthenticationInfo(client,client.getPassword(),salt,this.getName());
            return info;
        }
        return null;
    }

}
